import pymongo
import sys
from datetime import datetime
client = pymongo.MongoClient('mongodb://cs_user:cmcc5188@localhost:12018/cs_recharge')
db = client.cs_recharge
coll = db.rechange
line = datetime(2019,5,13).timestamp()
line2 = datetime(2019,5,20).timestamp()
res = coll.aggregate([
    {
        "$match": {
            # "merchantuid": "M2018082415341535096080",
            "orderstate": "0",
            #"tel_type": "unicom",
            # "wx_url": {"$regex": ".*payChannelCode=100203.*"}
            "time": {"$gt": line, "$lt": line2}
        }
    },
    
    #{"$project": {"sub": {"$subtract": ["$query_time", "$time"]}, "amount": "$amount"}},
    
    #{"$match": {"sub": {"$gt": 60*60}}},
    {
        "$group": {
            "_id": {"merchantid": "$merchantid", "tel_type": "$tel_type"},
            # "_id": None,
            "total": {
                "$sum": {"$toInt": "$amount"}
            }
        }
    }
])

res = list(res)
print(res)
# for h in range(0, 24):
#     for amount in ("30", "50", "100", "200"):
#         _id1 = {"hour": h, "orderstate": "0", "amount": amount}
#         _id2 = {"hour": h, "orderstate": "1", "amount": amount}
#         for i in res:
#             if i["_id"] == _id1:
#                 count1 = i["count"]
#             elif i["_id"] == _id2:
#                 count2 = i["count"]
#
#         print("%s点%.4f, %s, %s, %s" % (h, count1/(count1+count2), amount, count1, count1 + count2))
